package com.sobey.web.function.service.demo.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sobey.web.function.model.oracle.Demo;
import com.sobey.web.function.service.demo.DemoService;

@Service("demoService")
//启用注解事务，默认策略是所有方法都必须在事务中运行，必须声明在实现类上，接口声明无效
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, noRollbackFor = {Exception.class})
//不加事务会报异常：No Session found for current thread，所以不能使用@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class DemoServiceImpl implements DemoService {
	
	private static final Logger LOGGER = Logger.getLogger(DemoServiceImpl.class);
	
//	@Autowired
//	@Qualifier("mongoTemplate")
//	private MongoTemplate mongoTemplate;

	public List<Demo> findAll() {
		return null;
	}

	@Cacheable(value="commonCache", key="#id + 'DemoServiceImpl.getById'")
	public Demo getById(long id) {
		Demo  demo = new Demo();
		demo.setAge(1);
		demo.setCreateDate(new Date());
		demo.setId(1l);
		demo.setModifyDate(new Date());
		demo.setName("demo1");
		
//		save(null);
		
//		com.sobey.web.function.model.mongo.Demo demoM = (com.sobey.web.function.model.mongo.Demo) findById(null, 1);
		
		return demo;
	}
	
	
	
//	public Object findById(Class entityClass, Object id) {
//		
//		return this.mongoTemplate.findById("575fd6aaa66ae4d81df24c94", com.sobey.web.function.model.mongo.Demo.class);
//	}

	@CacheEvict(value="commonCache",key="#demo.id + 'DemoServiceImpl.getById'")
	public void save(Demo demo) {
		com.sobey.web.function.model.mongo.Demo demoM = new com.sobey.web.function.model.mongo.Demo();
		demoM.setName("ligang");
//		this.mongoTemplate.save(demoM);
	}
	
	@CacheEvict(value="commonCache",allEntries=true) 
	public void delete(Demo demo) {

	}
	
	@Cacheable(value="commonCache",key="#id + '_' + #name + 'DemoServiceImpl.getByName'")
	public Demo getByName(long id, String name) {
		return null;
	}

}
